package com.zbystudy;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created By zby on 14:17 2019/3/6
 * 这里用到的字符串排序
 */
public class StringOrder {


    /**
     * Created By zby on 15:07 2019/3/6
     * 升序拼接字符串
     */
    public static String ascConcatString(JSONObject jsonObject, String type) {
        List<String> keys = Sort.keySort(jsonObject,true);
        return StringConcat(jsonObject, type, keys);
    }


    /**
     * Created By zby on 15:07 2019/3/6
     * 降序拼接字符串
     */
    public static String descConcatString(JSONObject jsonObject, String type) {
        List<String> keys = Sort.keySort(jsonObject, false);
        return StringConcat(jsonObject, type, keys);
    }

    /**
     * Created By zby on 22:54 2019/3/6
     * <p>
     * 拼接字符串
     */
    private static String StringConcat(JSONObject jsonObject, String type, List<String> keys) {
        if (null == keys && keys.size() <= 0) {
            return null;
        }
        if (StringUtils.isBlank(type)) {
            type = "&";
        }
        StringBuilder concatBuilder = new StringBuilder();
        for (String key : keys) {
            concatBuilder.append(key + "=" + jsonObject.getString(key) + type);
        }
        return StringUtils.substring(concatBuilder.toString(), 0, concatBuilder.length() - 1);
    }
}
